package IOC.C2_resources.C2_3_resourcesImplementations.C2_3_2_resourcesImplementationsClasspathresource;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @author xufy
 * @version 1.0
 **/
public class ClassPathResourceTest {

    //resource that should be obtained from the classpath.
    // It uses either the thread context class loader,
    // a given class loader, or
    // a given class for loading resources.

    @Test
    public  void loadAndReadUrlResource() throws Exception {

        String path = "books.xml";
        ClassPathResource classPathResource = new ClassPathResource(path);
        // 读取spring-context.xml的内容
        InputStream ins2 = classPathResource.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int j;
        while ((j = ins2.read()) != -1) {
            bos.write(j);
        }
        System.out.println("读取的文件:" + classPathResource.getFilename() + ",内容:" + bos.toString());

    }
}
